<?php
namespace Home\Controller;

use Common\Controller\HomeController;

class PostController extends HomeController
{
    /**
     * 寄件列表
     * @author Mr.Alien
     * @return string
     */
    public function index($p = 1)
    {
        I('get.no') && ($this->where['no'] = array('like','%'.I('get.no').'%'));
        I('get.phone') && ($this->where['phone'] = array('like','%'.I('get.phone').'%'));
        I('get.name') && ($this->where['name'] = array('like','%'.I('get.name').'%'));
        $this->where['user_id'] = session('id');
        parent::index($p);
    }

    /**
     * 添加寄件
     * @author Mr.Alien
     * @return string
     */
    public function add()
    {
        $template_name_list = M('Price')->where(array('user_id'=>session('id')))->field("distinct template_name")->select();
        $this->assign('template_name_list',$template_name_list);
        $this->assign('province',D('Address')->getProvince());
        parent::add();
    }

    /**
     * 修改寄件
     * @author Mr.Alien
     * @param $id int 记录id
     * @return string
     */
    public function edit($id)
    {
        $info = M('Post')->where(array('id'=>(int)$id))->find();
        $template_name_list = M('Price')->where(array('user_id'=>session('id')))->field("distinct template_name")->select();
        $this->assign('template_name_list',$template_name_list);
        $this->assign('province',D('Address')->getProvince());
        if($info['sender_province']){
        $this->assign('sender_city',D('Address')->getChildren($info['sender_province']));
        $this->assign('sender_county',D('Address')->getChildren($info['sender_city']));
        $this->assign('sender_town',D('Address')->getChildren($info['sender_county']));
        $this->assign('sender_village',D('Address')->getChildren($info['sender_town']));
        }
        $this->assign('accept_city',D('Address')->getChildren($info['accept_province']));
        $this->assign('accept_county',D('Address')->getChildren($info['accept_city']));
        $this->assign('accept_town',D('Address')->getChildren($info['accept_county']));
        $this->assign('accept_village',D('Address')->getChildren($info['accept_town']));
        $this->assign('info',$info);
        $this->display();
    }

    /**
     * 保存寄件数据
     * @author Mr.Alien
     * @return string
     */
    public function save()
    {
        $data = I('post.');
        if($data['id']){
            $has = M('Post')->where(array('no'=>$data['no'],'id'=>array('neq',$data['id'])))->find();
            $has && $this->error('快递号重复');
            $res = M(CONTROLLER_NAME)->save($data);
        }else{
            $has = M('Post')->where(array('no'=>$data['no']))->find();
            $has && $this->error('快递号重复');
            M("Record")->where(array('user_id'=>session('id'),'no'=>$data['no']))->save(array('status'=>2));//标记快递号已使用
            $data['user_id'] = session('id');
            $res = M(CONTROLLER_NAME)->add($data);
        }
        if($res === false){
            $this->error('保存失败');
        }else{
            $has = M('Customer')->where(array('phone'=>$data['sender_phone'],'user_id'=>session('id')))->find();
            !$has && M('Customer')->add(array('phone'=>$data['sender_phone'],'name'=>$data['sender_name'],'user_id'=>session('id')));
            $has = M('Customer')->where(array('phone'=>$data['accept_phone'],'user_id'=>session('id')))->find();
            !$has && M('Customer')->add(array('phone'=>$data['accept_phone'],'name'=>$data['accept_name'],'user_id'=>session('id')));
            $this->success('保存成功');
        }
    }

    /**
     * 计算客户寄件价格
     * @author Mr.Alien
     * @return string
     */
    public function calculatePrice()
    {
        $data = I("post.");
        $price = M('Price')->where(array('user_id'=>session('id'),'province_id'=>$data['province_id'],'template_name'=>$data['template_name']))->find();
            if($data['weight'] < $price['range']){
                $this->success($price['price']);
            }else{
                $total = $price['price'] + ($data['weight']-$price['range'])*$price['excess_price'];
                $this->success($total);
            }

    }

    /**
     * 检测客户号码是否是会员
     * @author Mr.Alien
     * @param $phone string
     * @return string
     */
    public function member($phone)
    {
        $has = M('Customer')->where(array('user_id'=>session('id'),'phone'=>$phone))->find();
        $has ? $this->success() : $this->error();
    }

    /**
     * 待发货订单
     * @author Mr.Alien
     */
    public function deliver()
    {
        $list = M('Post')->where(array('status'=>1,'user_id'=>session('id')))->select();
        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 保存已发货信息
     * @author Mr.Alien
     */
    public function saveDeliver()
    {
        $data = I('post.');
        $res = M('Post')->where(array('id'=>array('in',$data['ids'])))->save(array('status'=>2));
        if($res === false){
            $this->error('保存失败');
        }else{
            $post_list = M('Post')->where(array('id'=>array('in',$data['ids']),'is_ren'=>2))->select();		
            foreach($post_list as $key => $value){
                $url = C('DOMAIN').'/Api/ApiList/updateSetSend?id='.$value['id'];
				$res = file_get_contents($url);				
            }
            $this->success('保存成功');
        }
    }

    /**
     * 输入订单号
     * @author Mr.Alien
     * @return string
     */
    public function writerNo()
    {
        $data = I('post.');
		$url = C('DOMAIN').'/Api/ApiList/FindGetSend?id='.$data['id'];
		$res = json_decode(file_get_contents($url),true);
		if($res['status'] != 1){
			$this->error($res['info']);
		}	
        $res = D("Post")->where(array('id'=>(int)$data['id']))->save(array('status'=>1));
        if($res === false){
            $this->error('保存失败');
        }else{
            $this->success('保存成功');
        }
    }

}